package com.my.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.my.demo.pojo.LoginLog;
import com.my.demo.pojo.Result;
import com.my.demo.pojo.User;
import com.my.demo.service.LoginLogService;
import com.my.demo.service.UserService;
import com.my.demo.shiro.JwtUtils;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {
  @Autowired
  UserService userService;
  @Autowired
  LoginLogService loginLogService;

  @PostMapping("/old_login")
  @CrossOrigin
  public Result old_login(@RequestBody User user, HttpSession session) {
    QueryWrapper<User> wrapper = new QueryWrapper<>();
    wrapper.eq("user_name", user.getUserName()); //name为 "Jone" 的用户
    wrapper.eq("password", user.getPassword());
    User isUser = userService.getOne(wrapper);

    Result result = new Result();
    if (isUser == null) {
      result.setSuccess(false);
      result.setMsg("登录失败");
    } else {
      result.setSuccess(true);
      result.setMsg("登录成功");
      result.setDetail(user);
      session.setAttribute("id", isUser.getUserId());

      //写入登录日志
      LoginLog loginLog = new LoginLog(null, isUser.getUserId(), null);
      loginLogService.save(loginLog);
    }
    return result;
  }

  @PostMapping("/login")
  @CrossOrigin
  public Result login(@RequestBody User user, ServletResponse response) {
    Result result = new Result();
    result.setSuccess(false);
    result.setDetail(null);
    System.out.println(user);

    String userName=user.getUserName();
    String password=user.getPassword();

    try {
      //获取主体对象
      Subject subject = SecurityUtils.getSubject();
      System.out.println("userName:"+userName+"  password:"+password);
      subject.login(new UsernamePasswordToken(userName, password));
      result.setSuccess(true);
      result.setMsg("登录成功");

      // 若登录成功，签发 JWT token
      String jwtToken = JwtUtils.sign(user.getUserName(), JwtUtils.SECRET);
      // 将签发的 JWT token 设置到 HttpServletResponse 的 Header 中
      ((HttpServletResponse) response).setHeader(JwtUtils.AUTH_HEADER, jwtToken);


      return result;
    } catch (UnknownAccountException e) {
      result.setMsg("用户名错误");
//      e.printStackTrace();
//      System.out.println("用户名错误!");
    } catch (IncorrectCredentialsException e) {
      result.setMsg("密码错误");
//      e.printStackTrace();
//      System.out.println("密码错误!");
    } catch (Exception e) {
      result.setMsg("登录失败");
    }
    return result;

  }

}
